﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Model;


namespace BLL.backStage
{
    public class NewsManager
    {
     
        private static int i = 1;
   
        public static Boolean addNews(Dictionary<int, string> newsdic)
        {
            i =i+1;
            Boolean flag = true;
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(HttpContext.Current.Server.MapPath("/backstage/NewsXML.xml"));
            XmlNode root = xmlDoc.SelectSingleNode("news新闻");//查找<Employees> 
            i = Int32.Parse(((XmlElement)root).GetAttribute("lastId"));
            i += 1;
            XmlElement xe1 = xmlDoc.CreateElement("Node");//创建一个<Node>节点 
            xe1.SetAttribute("id", i.ToString());

            XmlElement xesub1 = xmlDoc.CreateElement("title");
            xesub1.InnerText = newsdic[0];//设置文本节点 
            xe1.AppendChild(xesub1);//添加到<Node>节点中 
            XmlElement xesub2 = xmlDoc.CreateElement("type");
            xesub2.InnerText = newsdic[1];
            xe1.AppendChild(xesub2);
            XmlElement xesub3 = xmlDoc.CreateElement("date");
            xesub3.InnerText = newsdic[2];
            xe1.AppendChild(xesub3);
            XmlElement xesub4 = xmlDoc.CreateElement("content");
            xesub4.InnerText = newsdic[3];
            xe1.AppendChild(xesub4);

            root.AppendChild(xe1);//添加到<Employees>节点中 
            ((XmlElement)root).SetAttribute("lastId", i.ToString());
            xmlDoc.Save(HttpContext.Current.Server.MapPath("/backstage/NewsXML.xml"));
            return flag;
        }

        public static Boolean delNews(string id)
        {
            Boolean flag = true;
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(HttpContext.Current.Server.MapPath("/backstage/NewsXML.xml"));
            XmlNode root = xmlDoc.SelectSingleNode("news新闻");
            XmlNodeList xnl = xmlDoc.SelectSingleNode("news新闻").ChildNodes;
            for (int i = 0; i < xnl.Count; i++)
            {
                XmlElement xe = (XmlElement)xnl.Item(i);
                if (xe.GetAttribute("id") == id)
                {
                    root.RemoveChild(xe);
                    if (i < xnl.Count) 
                        i = i - 1;
                }
            }
            xmlDoc.Save(HttpContext.Current.Server.MapPath("/backstage/NewsXML.xml")); 
            return flag;

        }


        public static Boolean updNews(Dictionary<int, string> newsdic)
        {
            Boolean flag = true;
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(HttpContext.Current.Server.MapPath("/backstage/NewsXML.xml"));
            XmlNodeList nodeList = xmlDoc.SelectSingleNode("news新闻").ChildNodes;
            foreach (XmlNode xn in nodeList)//遍历所有子节点 
            {
                XmlElement xe = (XmlElement)xn; //将子节点类型转换为XmlElement类型 
                if(xe.GetAttribute("id")==newsdic[0])
                { 
                    XmlNodeList nls=xe.ChildNodes;//继续获取xe子节点的所有子节点 
                    foreach(XmlNode xn1 in nls)//遍历 
                    { 
                        XmlElement xe2=(XmlElement)xn1;//转换类型 
                        if(xe2.Name=="title")//如果找到 
                        { 
                            xe2.InnerText=newsdic[1];//则修改
                        } 

                         if(xe2.Name=="type")//如果找到 
                        { 
                            xe2.InnerText=newsdic[2];//则修改
                        } 

                          if(xe2.Name=="date")//如果找到 
                        { 
                            xe2.InnerText=newsdic[3];//则修改
                        } 

                          if(xe2.Name=="content")//如果找到 
                        { 
                            xe2.InnerText=newsdic[4];//则修改
                        } 
                     }  
                }
                xmlDoc.Save(HttpContext.Current.Server.MapPath("/backstage/NewsXML.xml"));//保存。
            }
            return flag;
        }

        public static List<News> searchNews(string title, string type)
        {
            List<News> newslist = new List<News>();
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(HttpContext.Current.Server.MapPath("/backstage/NewsXML.xml"));
            XmlNodeList nodeList = xmlDoc.SelectSingleNode("news新闻").ChildNodes;
             foreach (XmlNode xn in nodeList)//遍历所有子节点 
            {
                XmlElement xe = (XmlElement)xn; //将子节点类型转换为XmlElement类型 
                News news = new News();
                Boolean flag = false;
                flag = isFetch(xe,title,type);
                if (flag == true)
                {
                    news.Id = xe.GetAttribute("id");
                    XmlNodeList nls = xe.ChildNodes;
                    foreach (XmlNode xn1 in nls)//遍历 
                    {
                        XmlElement xe2 = (XmlElement)xn1;//转换类型 
                        if (xe2.Name == "title") 
                        {
                           news.Title = xe2.InnerText;
                        }

                        if (xe2.Name == "type")
                        {
                            news.Type = xe2.InnerText;
                        }

                        if (xe2.Name == "date")
                        {
                           news.Date = xe2.InnerText;
                        }

                        if (xe2.Name == "content")
                        {
                            news.Content = xe2.InnerText;
                        }
                    }  
                    newslist.Add(news);
                 }

              }
             //xmlDoc.Save(HttpContext.Current.Server.MapPath("/backstage/NewsXML.xml"));//保存。
             return newslist;
            }


        public static Boolean isFetch(XmlElement xe,string title,string type){
            Boolean flag = false;
            XmlNodeList nls = xe.ChildNodes;
            if(title!=null&&title.Length>0){
                if(type!=null&&type.Length>0){
                    Boolean isvaild1 = false;
                    Boolean isvaild2 = false;
                    foreach (XmlNode xn1 in nls)
                    {
                        XmlElement xe2 = (XmlElement)xn1;
                        if (xe2.Name == "title" && xe2.InnerText == title)
                        {
                            isvaild1 = true;
                        }
                        if (xe2.Name == "type" && xe2.InnerText == type)
                        {
                            isvaild2 = false;
                        }

                    }
                    if (isvaild1 && isvaild2)
                    {
                       flag = true;
                    }
                }else{
                    foreach (XmlNode xn1 in nls)
                    {
                        XmlElement xe2 = (XmlElement)xn1;
                        if (xe2.Name == "title" && xe2.InnerText == title)
                        {
                            flag = true;
                        }
                    }
                }
            }else{
                
                if(type!=null&&type.Length>0){

                    foreach (XmlNode xn1 in nls)
                    {
                        XmlElement xe2 = (XmlElement)xn1;
                        if (xe2.Name == "type" && xe2.InnerText == type)
                        {
                            flag = true;
                        }
                    }
                }else{
                    flag = true;
                }
            }

            return flag;
        }

        public static int FillSearvhTable(Table table,string title, string type,int pageNo)
        {
            List<News> newslist = searchNews(title,type);
            int beginItemNo = (pageNo - 1) * 5;
            int endItemNo = ((pageNo - 1) * 5 + 5) < (newslist.Count) ? ((pageNo - 1) * 5 + 5) : (newslist.Count);
            for (int i = beginItemNo; i < endItemNo; ++i)
            {
                News news = newslist[i];

                TableRow tr = new TableRow();
                tr.BackColor = System.Drawing.Color.White;

                TableCell[] tc = new TableCell[6];

                for (int j = 0; j < 6; ++j)
                {
                    tc[j] = new TableCell();
                }

                tc[0].Text = news.Id;
                tc[0].Height = 20;
                tc[0].VerticalAlign = VerticalAlign.Middle;
                tc[0].HorizontalAlign = HorizontalAlign.Center;
                tr.Cells.Add(tc[0]);


                tc[1].Text = news.Title;
                tc[1].VerticalAlign = VerticalAlign.Middle;
                tc[1].HorizontalAlign = HorizontalAlign.Center;
                tr.Cells.Add(tc[1]);


                tc[2].Text = news.Type;
                tc[2].VerticalAlign = VerticalAlign.Middle;
                tc[2].HorizontalAlign = HorizontalAlign.Center;
                tr.Cells.Add(tc[2]);

                tc[3].Text = news.Date;
                tc[3].VerticalAlign = VerticalAlign.Middle;
                tc[3].HorizontalAlign = HorizontalAlign.Center;
                tr.Cells.Add(tc[3]);

                tc[4].Text = "<a href='./showNews.aspx?newsId=" + news.Id + "'>查看|</a><a href='./updateNews.aspx?newsId=" + news.Id + "'>编辑|</a><a href='./delNews.aspx?newsId="+news.Id+"&pageNo ="+pageNo+"'>删除</a>";
                tc[4].VerticalAlign = VerticalAlign.Middle;
                tc[4].HorizontalAlign = HorizontalAlign.Center;
                tr.Cells.Add(tc[4]);

                table.Rows.Add(tr);
            }
            TableRow pageNoTr = new TableRow();
            TableCell pageNoTc = new TableCell();
            int TotalPageNum = (newslist.Count / 5 + 1);
            String pageNoStr = "第" + pageNo + " / " + TotalPageNum + " 页";
            for (int j = 0; j < TotalPageNum; ++j)
            {
                if (j + 1 != pageNo)
                {
                    // pageNoStr += "<a href='./searchResult.aspx?keyType=" + userName + "&keyWord=" + keyWord + "&pageNo=" + (j + 1) + "'>[" + (j + 1) + "]</a>&nbsp;";
                }
                else
                {
                    pageNoStr += " " + (j + 1) + " ";
                }
            }
            pageNoTc.HorizontalAlign = HorizontalAlign.Center;
            pageNoTc.Text = pageNoStr;
            pageNoTr.Cells.Add(pageNoTc);
            table.Rows.Add(pageNoTr);

            return newslist.Count;
        }

        public static Dictionary<int, string> getNewsById(string id)
        {
            Dictionary<int, string> news = new Dictionary<int, string>();
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(HttpContext.Current.Server.MapPath("/backstage/NewsXML.xml"));
            XmlNodeList nodeList = xmlDoc.SelectSingleNode("news新闻").ChildNodes;
            foreach (XmlNode xn in nodeList)
            {
                XmlElement xe = (XmlElement)xn;
                if (xe.GetAttribute("id") == id)
                {
                    XmlNodeList xnl = xe.ChildNodes;
                    foreach (XmlNode xn1 in xnl)
                    {
                        XmlElement xe2 = (XmlElement)xn1;
                        if (xe2.Name == "title")
                        {
                            news.Add(1, xe2.InnerText);
                        }

                        if (xe2.Name == "type")
                        {
                            news.Add(2,xe2.InnerText);
                        }

                        if (xe2.Name == "date")
                        {
                            news.Add(3,xe2.InnerText);
                        }

                        if (xe2.Name == "content")
                        {
                             news.Add(4, xe2.InnerText);
                        }
                    }
                }

            }
            return news;
        }
    }
}
